﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Bohdan.Arch.Messaging
{
    public abstract class GeneralExitMessage : GeneralMessage
    {
        private EventWaitHandle _ewhCanExit = new EventWaitHandle(false, EventResetMode.ManualReset);

        public event Action OnExitProcessingStarted;

        public GeneralExitMessage()
        {
            OnExitProcessingStarted += () => _ewhCanExit.Set();
        }

        internal void FireOnExitProcessingStarted()
        {
            if (OnExitProcessingStarted != null)
                OnExitProcessingStarted();
        }

        public override void DesclareMessageProcessed()
        {
        }

        public override bool IsProcessed
        {
            get
            {
                return false;
            }
        }

        public override bool WaitCompleted()
        {
            return _ewhCanExit.WaitOne();
            //return false;
        }

        public override bool WaitCompleted(TimeSpan ts)
        {
            return _ewhCanExit.WaitOne(ts);
            //return false;
        }
    }
}
